package yxy.game.pm2.command.dungeon;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.BattleMessage;
import yxy.game.pm2.message.DungeonMessage;
import yxy.game.pm2.module.dungeon.DungeonHandle;

/** 日常副本 副本挑战战斗开始 */
@MessageType(proto = Command.DungeonChanllengeExecute, classtype = DungeonMessage.DungeonChanllengeExecuteRequest.class)
public class CMD_DungeonChanllengeExecute extends Command<DungeonMessage.DungeonChanllengeExecuteRequest> {

	@Override
	public void execute(MessageSource messageSource, DungeonMessage.DungeonChanllengeExecuteRequest request) {
		String playerCode = messageSource.getTag();
		BattleMessage.BattleExecuteRequest battleReq = request.getBattleReq();

		DungeonMessage.DungeonChanllengeExecuteResponse.Builder response = DungeonMessage.DungeonChanllengeExecuteResponse.newBuilder();

		DungeonHandle handle = new DungeonHandle();
		if (handle.isBattling(playerCode)) {
			send(messageSource, response, Result.BattleIsRunning);// 战斗尚未结束
			return;
		}

		try {
			response.setBattleResp(handle.battleResponseExecute(playerCode, battleReq));
			send(messageSource, response, Result.Success);
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		}
	}

	private void send(MessageSource messageSource, DungeonMessage.DungeonChanllengeExecuteResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}